import App from './App'
import { createSSRApp } from 'vue'

// 创建应用实例
export function createApp() {
  const app = createSSRApp(App)
  
  // 配置compilerOptions，将uni-开头的标签识别为自定义元素
  app.config.compilerOptions = {
    isCustomElement: (tag) => tag.startsWith('uni-')
  }
  
  // 全局错误处理
  app.config.errorHandler = (err, instance, info) => {
    console.error('应用错误:', err, info)
    // 这里可以添加错误上报逻辑
  }
  
  // 全局属性设置
  app.config.globalProperties.$formatDate = (date, format) => {
    const d = new Date(date)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')
    const hours = String(d.getHours()).padStart(2, '0')
    const minutes = String(d.getMinutes()).padStart(2, '0')
    const seconds = String(d.getSeconds()).padStart(2, '0')
    
    return (format || 'YYYY-MM-DD HH:mm:ss')
      .replace('YYYY', year)
      .replace('MM', month)
      .replace('DD', day)
      .replace('HH', hours)
      .replace('mm', minutes)
      .replace('ss', seconds)
  }
  
  // 添加uni-app路由配置
  app.config.globalProperties.$router = {
    push: (url, options) => uni.navigateTo({ url, ...options }),
    replace: (url, options) => uni.redirectTo({ url, ...options }),
    back: (delta = 1) => uni.navigateBack({ delta }),
    switchTab: (url, options) => uni.switchTab({ url, ...options }),
    reLaunch: (url, options) => uni.reLaunch({ url, ...options })
  }
  
  // 全局挂载uni对象便于组件内使用
  app.config.globalProperties.$uni = uni
  
  return {
    app
  }
}